/**
 * @author Luc Yriarte
 * 
 * Licence : BSD http://www.opensource.org/licenses/bsd-license.php
 *  
 */
package basicSim;

import java.awt.Component;

public class Animation extends Thread {
	public int nObjects, millisec;

	Object3D object[];
	TransformList trans[];

	public Component view;
	public SimView objView;

	public Animation(Component v, SimView oV, int nO, 
			Object3D o[], TransformList tr[], int ms) {
		super();
		view = v;
		objView = oV;
		nObjects = nO;
		object = o;
		trans = tr;
		millisec = ms;
System.out.println("New animation " + nO + " objects, step " + ms + " ms");
	}

	public Animation append(Animation a) {
		Object3D obj[];
		TransformList trl[];
		int n = a.nObjects + nObjects;
		obj = new Object3D[n];
		trl = new TransformList[n];
		int i;
		for (i = 0; i < nObjects; i++) {
			obj[i] = object[i];
			trl[i] = trans[i];
		}
		for (i = nObjects; i < n; i++) {
			obj[i] = a.object[nObjects - i];
			trl[i] = a.trans[nObjects - i];
		}
System.out.println("Animation append " + nObjects + " + " + a.nObjects + " objects");
		return new Animation(view, objView, n, obj, trl, millisec);
	}

	public void run() {
		int i;
		boolean changed = true;
System.out.println("Animation running");
		while (changed && nObjects != 0) {
			changed = false;
			for (i = 0; i < nObjects; i++) {
				if (trans[i] != null) {
					changed = true;
					object[i].transform(trans[i].transform);
					trans[i] = trans[i].next();
				}
			}
			if (changed) {
//System.out.println("Animation view.repaint");
				objView.updateModel();
				view.repaint();
			}
			try {
				sleep(millisec);
			} catch (Exception e) {
				System.out.println("Animation exception " + e.toString());
			}
		}
System.out.println("Animation finished");
	}

}
